package com.gxa.shop.servlet.account;



import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.pojo.Account;
import com.gxa.shop.commons.r.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AdminInfo",value = "/account/info")
public class AccountInfo extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获得前端传过来的token
        String token=req.getParameter("token");
        System.out.println(token);
        Account account = (Account) getServletContext().getAttribute(token);

        PrintWriter writer = resp.getWriter();
        //结果集
        R r = null;
        //判断是否登录成功
        if(account==null){
            r=R.error("没有登录");
        }else{
            if (account.getRoleId()==1){
                r=R.ok().addData("account",account);
            }else if (account.getRoleId()==2){
                r=R.ok().addData("account",account.getUsername());
            }

        }
        String result = JSON.toJSONString(r);
        System.out.println("UserLoginServlet:"+result);
        writer.write(result);
        writer.flush();
        writer.close();
    }
}
